package mx.com.mindbits.bandpay.adapters;

import java.util.ArrayList;
import java.util.List;

import mx.com.mindbits.bandpay.R;
import mx.com.mindbits.bandpay.utils.ConstantsValues;
import mx.com.mindbits.dao.contacts;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class ContactAdapter extends ArrayAdapter<contacts>{
	
	
	private Context context;
	private ArrayList<contacts> item;
	private List<contacts> _contactsA;
	int pos;
		
	public ArrayList<contacts> get_contactsA() {
		return item;
	}


	
	public ContactAdapter(Context context,
			List<contacts> items) {
		super(context, R.layout.item_contacto,items);		
		this.context = context;
		this.item = new ArrayList<contacts>();
		item.addAll(items);
		
		Log.i(ConstantsValues.TAG_DAO, "Constructor add " );
		
	}
	
	static class ListHolder{
		TextView nombre;
		TextView email;
		ImageView imagen;
		CheckBox select_ch;
	}
	
	public View getView(int position, View cView, ViewGroup group){
		
		ListHolder listHolder =null;
		
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				
		if(cView == null){
			listHolder = new ListHolder();
			cView = inflater.inflate(R.layout.item_contacto, null);
			listHolder.nombre = (TextView) cView.findViewById(R.id.tv_nombre);
			listHolder.email = (TextView) cView.findViewById(R.id.tv_email);
			listHolder.imagen = (ImageView) cView.findViewById(R.id.img_imagen);
			listHolder.select_ch = (CheckBox) cView.findViewById(R.id.ch_contact);
			cView.setTag(listHolder);
			
			listHolder.select_ch.setOnClickListener(new View.OnClickListener(){

				@Override
				public void onClick(View v) {
					CheckBox ch = (CheckBox) v;
					contacts _contacts = (contacts) ch.getTag();
					_contacts.setContact_visible(!ch.isChecked());
					
					Log.i(ConstantsValues.TAG_DAO, "<<<checked>>>> " + _contacts.getContact_name() 
							+ _contacts.getContact_visible());
					
				}
				
			});
			
		}else{
			listHolder = (ListHolder) cView.getTag();
			
		}
		contacts _contacts = item.get(position);
		Log.i(ConstantsValues.TAG_DAO, "<<<c>>>> " + _contacts.getContact_name()  + _contacts.getContact_visible());		
		listHolder.nombre.setText(_contacts.getContact_name());
		listHolder.email.setText(_contacts.getContact_email());
		listHolder.select_ch.setSelected(_contacts.getContact_visible());
		listHolder.select_ch.setTag(_contacts);
		
		
		return cView;
	}

}
